package com.nuc.bigdata.service.impl;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.nuc.bigdata.exception.BusinessException;
import com.nuc.bigdata.model.ResultCode;
import com.nuc.bigdata.orm.dto.NewsCommentsDto;
import com.nuc.bigdata.orm.entity.CommodityComments;
import com.nuc.bigdata.orm.entity.CommodityCommentsLike;
import com.nuc.bigdata.orm.feign.CommodityCommentsFeign;
import com.nuc.bigdata.orm.mapper.CommodityCommentsLikeMapper;
import com.nuc.bigdata.orm.mapper.CommodityCommentsMapper;
import com.nuc.bigdata.service.CommodityCommentsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.Date;

/**
 * @author JackLuo
 * @date 2019/12/3
 */
@Service
public class CommodityCommentsServiceImpl implements CommodityCommentsService {

    private final CommodityCommentsMapper commentsMapper;
    private final CommodityCommentsLikeMapper likeMapper;
    private static final Long LENGTH = 3L;

    @Autowired
    CommodityCommentsServiceImpl(CommodityCommentsMapper commentsMapper,
                                 CommodityCommentsLikeMapper likeMapper) {
        this.commentsMapper = commentsMapper;
        this.likeMapper = likeMapper;
    }

    /**
     * 根据商品唯一标识获取商品评论feign
     *
     * @param commodityId 商品唯一标识
     * @return 包含了评论feign的ArrayList
     */
    @Override
    public ArrayList<CommodityCommentsFeign> listById(String commodityId,Long pageSize) {
        Long offset = (pageSize -1) * LENGTH;
        ArrayList<CommodityComments> commodityComments = commentsMapper.listByCommodityId(commodityId, offset, LENGTH);
        ArrayList<CommodityCommentsFeign> feignArrayList = new ArrayList<>();
        commodityComments.forEach(item -> feignArrayList.add(transformComments(item)));
        return feignArrayList;
    }

    private CommodityCommentsFeign transformComments(CommodityComments comments) {
        CommodityCommentsFeign feign = new CommodityCommentsFeign();
        feign.setComment(comments.getComment());
        feign.setCommentLike(comments.getCommentLike());
        feign.setCommodityCommentId(comments.getCommodityCommentId());
        feign.setCommodityId(comments.getCommodityId());
        feign.setCreatedBy(comments.getCreatedBy());
        feign.setCreateTime(comments.getCreateTime());
        return feign;
    }

    /**
     * 根据商品唯一标识创建商品评论
     *
     * @param commodityId 商品唯一标识
     * @param commentsDto 商品评论dto
     */
    @Override
    @Transactional(rollbackForClassName = "Exception")
    public void createComments(String commodityId, NewsCommentsDto commentsDto) {
        CommodityComments comments = new CommodityComments();
        comments.setComment(commentsDto.getContent());
        comments.setCommodityId(commodityId);
        String uuid = IdUtil.fastSimpleUUID();
        comments.setCommodityCommentId(uuid);
        comments.setCreatedBy(commentsDto.getUserId());
        comments.setCreateTime(new Date());
        commentsMapper.insert(comments);
    }


    /**
     * 点赞指定评论
     *
     * @param commodityCommentId 商品评论唯一标识
     * @param userId             用户唯一标识
     */
    @Override
    @Transactional(rollbackForClassName = "Exception")
    public void toCommentLike(String userId, String commodityCommentId) {
        QueryWrapper<CommodityCommentsLike> qw = new QueryWrapper<>();
        qw.eq("commodity_comment_id",commodityCommentId );
        qw.eq("created_by", userId);
        CommodityCommentsLike like = likeMapper.selectOne(qw);
        if (ObjectUtils.isEmpty(like)) {
            CommodityCommentsLike commentsLike = new CommodityCommentsLike();
            commentsLike.setCommodityCommentId(commodityCommentId);
            commentsLike.setCreatedBy(userId);
            likeMapper.insert(commentsLike);
        }else {
            throw new BusinessException(ResultCode.COMMENT_LIKE_IS_EXIST,like);
        }
    }
}
